<template>
  <div>
    <!-- 表格 -->
    <el-table :data="skuData" style="width: 100%" border>
      <el-table-column
        type="index"
        label="序号"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="skuName"
        label="名称"
        width="width"
      ></el-table-column>
      <el-table-column
        prop="skuDesc"
        label="描述"
        width="width"
      ></el-table-column>
      <el-table-column prop="prop" label="默认图片" width="120"
        ><template slot-scope="{ row }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 100px"
          /> </template
      ></el-table-column>
      <el-table-column prop="weight" label="重量" width="80"></el-table-column>
      <el-table-column prop="price" label="价格" width="80"></el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{ row }">
          <hint-button
            type="success"
            icon="el-icon-top"
            title="上架"
            size="mini"
            v-if="row.isSale == 0"
            @click="sale(row)"
          ></hint-button>
          <hint-button
            v-else
            type="info"
            icon="el-icon-bottom"
            title="下架"
            size="mini"
            @click="cance(row)"
          ></hint-button>
          <hint-button
            type="primary"
            icon="el-icon-edit"
            title="修改"
            size="mini"
            @click="primary"
          ></hint-button>
          <hint-button
            type="info"
            icon="el-icon-info"
            title="查看"
            size="mini"
            @click="showSku(row)"
          ></hint-button>
          <hint-button
            type="danger"
            icon="el-icon-delete"
            title="删除"
            size="mini"
          ></hint-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分頁器 -->
    <el-pagination
      style="text-align: center"
      @size-change="handleSizeChange"
      @current-change="getData"
      :current-page="page"
      :page-sizes="[10, 20, 50]"
      :page-size="limit"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
    >
    </el-pagination>
    <!-- 抽屉 -->
    <el-drawer :visible.sync="drawer" :with-header="false" size="50%">
      <el-row>
        <el-col :span="5">名称 </el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <el-tag
            type="success"
            v-for="(item, index) in skuInfo.skuAttrValueList"
            :key="index"
            style="margin-right: 10px"
            >{{ item.attrId }}--{{ item.valueId }}</el-tag
          >
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16"
          ><el-carousel :interval="5000" arrow="always" height="500px">
            <el-carousel-item v-for="(item,index) in skuInfo.skuImageList" :key="index">
              <img :src="item.imgUrl" style="width:100%">
            </el-carousel-item>
          </el-carousel></el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      skuData: [], // 数据列表
      page: 1,
      limit: 5,
      total: null,
      drawer: false, // 控制抽屉的显示和隐藏
      skuInfo: {}, // sku详细信息
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    // 获取数据列表
    async getData(pages = 1) {
      this.page = pages;
      let res = await this.$API.sku.reqSkuList(this.page, this.limit);
      if (res.code == 200) {
        this.total = res.data.total;
        this.skuData = res.data.records;
      }
    },

    handleSizeChange(limit) {
      this.limit = limit;
      this.getData(this.page);
    },
    // 上架
    async sale(row) {
      let res = await this.$API.sku.reqSale(row.id);
      if (res.code == 200) {
        row.isSale = 1; // 用来标识商品上架状态
        this.$message({
          type: "success",
          message: "上架成功",
        });
      }
    },
    // 下架
    async cance(row) {
      let res = await this.$API.sku.reqCance(row.id);
      if (res.code == 200) {
        row.isSale = 0;
        this.$message("下架成功");
      }
    },
    // 修改
    primary() {
      this.$message("正在开发中......");
    },
    // 查看
    async showSku(row) {
      let res = await this.$API.sku.reqSkuById(row.id);
      if (res.code == 200) {
        this.skuInfo = res.data;
        this.drawer = true;
      }
    },
  },
};
</script>
<style>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 18px;
    opacity: 0.75;
    line-height: 300px;
    margin: 0;
  }
  
  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }
  
  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
  /* 把轮播图小球的样式放到scoped里面并不生效*/
  /* 既要写在scoped里面又想影响到子组件那就要用到深度选择器
     '>>>' ：原生的css深度选择器
     /deep/ : 一般用于less
     ::v-deep : 一般用于scss
   */
  .el-carousel__button{
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: red;
  }
</style>
<style scoped>
.el-row {
  margin: 20px 10px;
}
.el-row .el-col-5 {
  font-size: 28px;
  font-weight: 700;
  text-align: right;
  margin-right: 15px;
}
.el-row .el-col-16 {
  font-size: 26px;
}
</style>